/**
 * Created with JetBrains PhpStorm.
 * User: Administrator
 * Date: 13-5-3
 * Time: 下午12:55
 * To change this template use File | Settings | File Templates.
 */
Date.prototype.format = function(formatStr)
{
    var str = formatStr;
    var Week = ['日','一','二','三','四','五','六'];
    str=str.replace(/yyyy|YYYY/,this.getFullYear());
    str=str.replace(/yy|YY/,(this.getYear() % 100)>9?(this.getYear() % 100).toString():'0' + (this.getYear() % 100));
    str=str.replace(/MM/,this.getMonth()>8?(this.getMonth()+1).toString():'0' + (this.getMonth()+1));
    str=str.replace(/M/g,this.getMonth());
    str=str.replace(/w|W/g,Week[this.getDay()]);
    str=str.replace(/dd|DD/,this.getDate()>9?this.getDate().toString():'0' + this.getDate());
    str=str.replace(/d|D/g,this.getDate());
    str=str.replace(/hh|HH/,this.getHours()>9?this.getHours().toString():'0' + this.getHours());
    str=str.replace(/h|H/g,this.getHours());
    str=str.replace(/mm/,this.getMinutes()>9?this.getMinutes().toString():'0' + this.getMinutes());
    str=str.replace(/m/g,this.getMinutes());
    str=str.replace(/ss|SS/,this.getSeconds()>9?this.getSeconds().toString():'0' + this.getSeconds());
    str=str.replace(/s|S/g,this.getSeconds());
    return str;
}
exports.Date = Date;
var fs = require("fs");
exports.workPath = fs.dirname(fs.absolute(phantom.casperScript));
exports.noop = function(){};
exports.log2f = function(path,content,mode,dateFormat){
    mode = mode||'a';
    if(!dateFormat && dateFormat!==false)dateFormat = 'yyyy-MM-dd HH:mm:ss';
    var dateStr = dateFormat===false?'':"["+new Date().format('yyyy-MM-dd HH:mm:ss')+"]: ";
    content = dateStr+content+"\n";
    fs.write(path,content,mode);
}
exports.log2c = function(content){
    console.log(content);
}
exports.log2fc = function(path,content,mode,dateFormat){
    exports.log2f(path,content,mode,dateFormat);
    exports.log2c(content);
}
exports.read = function(filepath,create){
    create = create || true;
    var content = '';
    if(!(!fs.exists(filepath) && !create)){
        content = fs.read(filepath);
    }

    return content;
}
exports.getDefaultCfg = function (argument) {
    return {
        verbose: true,
        logLevel: 'debug',
        pageSettings: {
            loadImages:  true,         // The WebPage instance used by Casper will
            loadPlugins: true,         // use these settings
            userAgent: 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17'
        },
        viewportSize:{width: 1680, height: 924}
    };
}
exports.getCasper = function(cfg){
    cfg = cfg||exports.getDefaultCfg();
    var casper = require('casper').create(cfg);
    var os = require('system').os;
    if(os.name=='windows'){
        phantom.outputEncoding="gbk";// this is for window
    }

   return casper;
}
exports.getScriptName = function () {
    // var scriptName = '';
    // var args = require('system').args;
    // console.log("args in util:"+args);
    // var len = args.length;
    // if(len){
    //     scriptName = args[len-1];
    // }
    // return scriptName;
    return phantom.casperScript;
}

exports.cookie2f = function(filepath,cookies) {
    exports.log2f(filepath,JSON.stringify(cookies),'w',false);
}

